/**
 * electron-store 存储数据
 */
const Store = require("electron-store");
const path = require("path");
const fs = require("fs");

const possibleDownloadPaths = [
  path.join("E:", "kingshineDownload"), // 优先尝试E盘
  path.join("D:", "kingshineDownload"), // 如果E盘不存在，尝试D盘
  path.join("C:", "kingshineDownload"), // 如果D盘也不存在，使用C盘
];

// 检查并选择第一个存在的路径
let downloadPath = possibleDownloadPaths.find((p) => {
  const drive = p.split(path.sep)[0];
  return fs.existsSync(drive);
});

// 如果所有路径都不可用，回退到操作系统默认的下载路径
if (!downloadPath) {
  downloadPath = path.join(
    require("os").homedir(),
    "Downloads",
    "kingshineDownload"
  );
}

const configOption = {
  downloadconcount: 3,
  uploadcontcount: 3,
  downloadtransrate: 128,
  uploadtransrate: 128,
  dlIsLimit: false, // 下载是否限制速度
  ulIsLimit: false, // 上传是否限制速度
  downloadPath, // 下载位置
};

const store = new Store();

if (!store.has("configOption")) {
  store.set("configOption", configOption);
  if (downloadPath && !fs.existsSync(downloadPath)) {
    fs.mkdirSync(downloadPath);
  }
}

module.exports = store;
